﻿'
' Boogaloo® - http://www.mindfly.com
' Copyright (c) 2008-2009
' by Mindfly, Inc.
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
' INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
' DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
' SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
' SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
' WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
' THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Imports System.IO
Imports System.Net
Imports System.Xml
Imports System.Text
Imports Boogaloo.Base
Imports Boogaloo.Syndication.XmlRpc

Namespace Ping


    Public Class Pingback

        ''' <summary>
        ''' Sends pingback to website
        ''' </summary>
        Public Shared Sub Send(ByVal source_url As Uri, ByVal target_url As Uri)

            If Not CoreSettings.APIs("pingback").expose Then Exit Sub
            If source_url Is Nothing Or target_url Is Nothing Then Exit Sub

            Try

                Dim req As HttpWebRequest = HttpWebRequest.Create(target_url)
                req.Credentials = CredentialCache.DefaultNetworkCredentials

                Dim resp As HttpWebResponse = req.GetResponse()

                Dim ping_url As String = String.Empty
                If Not String.IsNullOrEmpty(resp.Headers("x-pingback")) Then
                    ping_url = resp.Headers("x-pingback")
                ElseIf Not String.IsNullOrEmpty(resp.Headers("pingback")) Then
                    ping_url = resp.Headers("pingback")
                End If

                Dim url As Uri = Nothing
                If Not String.IsNullOrEmpty(ping_url) And Uri.TryCreate(ping_url, UriKind.Absolute, url) Then

                    Dim rpc As New XmlRpcRequest
                    rpc.methodName = "pingback.ping"
                    rpc.params.Add(New XmlRpcValue(source_url.ToString(), XmlRpcValue.ValueType.str))
                    rpc.params.Add(New XmlRpcValue(target_url.ToString(), XmlRpcValue.ValueType.str))
                    rpc.Send(url)

                End If

            Catch ex As Exception
                If CoreSettings.EnableLogging Then EventLogger.Write(ex.Message, "Ping.PingBack", True)
            End Try

        End Sub


        Private Shared Sub AddXmlToRequest(ByVal source_url As Uri, ByVal target_url As Uri, ByVal req As HttpWebRequest)

            Dim st As Stream = req.GetRequestStream()

            Using writer As XmlTextWriter = New XmlTextWriter(st, Encoding.ASCII)

                writer.WriteStartDocument(True)
                writer.WriteStartElement("methodCall")

                writer.WriteElementString("methodName", "pingback.ping")
                writer.WriteStartElement("params")

                writer.WriteStartElement("param")
                writer.WriteStartElement("value")
                writer.WriteElementString("string", source_url.ToString())
                writer.WriteEndElement()
                writer.WriteEndElement()

                writer.WriteStartElement("param")
                writer.WriteStartElement("value")
                writer.WriteElementString("string", target_url.ToString())
                writer.WriteEndElement()
                writer.WriteEndElement()

                writer.WriteEndElement() ' params
                writer.WriteEndElement() ' methodCall

            End Using

        End Sub

    End Class

End Namespace